package com.servlet.music;

import cn.hutool.core.convert.impl.DateConverter;
import com.dao.CommentService;
import com.dao.impl.CommentServiceImpl;
import com.entity.Comment;
import com.entity.vo.CommentVo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.servlet.teacher.BaseServlet;
import com.util.JsonUtil;
import com.util.PageUtil;
import com.util.ProxyUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;


@WebServlet("/CommentServlet")
public class CommentServlet extends BaseServlet {

	/**
	 * 根据id进行更新
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		CommentService commentService = (CommentService)ProxyUtil.getDaoProxy(new CommentServiceImpl());

		req.removeAttribute("createTime");
		req.removeAttribute("updateTime");

		try {
			Comment comment = new Comment();
			BeanUtils.populate(comment, req.getParameterMap());

			int update = commentService.update(comment);

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		out.print(gson.toJson(jsonUtil));
	}

	/**
	 * 批量删除
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deleteIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		CommentService commentService = (CommentService)ProxyUtil.getDaoProxy(new CommentServiceImpl());

		String id = req.getParameter("ids");
		Comment comment = new Comment();

		System.out.println(id);
		try {
			if(id.endsWith(",")) {
				id = id.substring(0, id.length()-1);
			}

			String[] split = id.split(",");

			int i = commentService.deletedIds(split);

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		out.print(gson.toJson(jsonUtil));

	}

	/**
	 * 根据id查询某一条评论
	 *
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void queryData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		CommentService commentService = (CommentService)ProxyUtil.getDaoProxy(new CommentServiceImpl());

		String id = req.getParameter("id");
		Comment comment = new Comment();

		System.out.println(id);
		try {
			comment.setId(Integer.parseInt(id));
			List<Comment> comments = commentService.queryData(comment);
			if (!comments.isEmpty()) {
				comment = comments.get(0);
			}

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		jsonUtil.setData(comment);
		out.print(gson.toJson(jsonUtil));

	}

	/**
	 * 内容查询分页
	 *
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void queryDataBypage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		Comment comment = new Comment();

		String id = req.getParameter("id");

		CommentService commentService = (CommentService)ProxyUtil.getDaoProxy(new CommentServiceImpl());

		PageUtil<CommentVo> commentPageUtil = null;

		//日期格式转换器yyyy-MM-dd转换成data类型(很重要)
		ConvertUtils.register(new DateLocaleConverter(), Date.class);

		try {
			BeanUtils.populate(comment, req.getParameterMap());
			commentPageUtil = commentService.queryDataBypage2(Integer.parseInt(req.getParameter("pageNum")),
					Integer.parseInt(req.getParameter("pageSize")), comment);
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		jsonUtil.setData(commentPageUtil);
		out.print(gson.toJson(jsonUtil));
	}


}
